import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../../../scene/events/PlayerEvent";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { AutoFightRule } from "../../../rules/AutoFightRule";
import { Cache } from "../../../cache/Cache";
import { AIManager } from "../../../scene/ai/AIManager";
import { AIType } from "../../../scene/ai/AIType";
//class MagicTowerAutoPath
    
    export  class MagicTowerAutoPath
    {
        constructor()
        {
            
            if (MagicTowerAutoPath._instance) 
            {
                throw new Error("MagicTowerAutoPath 单例");
            }
            return;
        }

        public startCountDown(): void
        {
            this.startTimer();
            RolePlayer.instance.addEventListener(PlayerEvent.ON_WALK, this.onPlayerWalkHandler);
            return;
        }

        public onPlayerWalkHandler(arg1: PlayerEvent): void
        {
            this.stopTimer();
            return;
        }

        /* internal  */startTimer(): void
        {
            if (!this._timer) 
            {
                this._timer = new SecTimer(1, 30);
                this._timer.addListener(TimerType.COMPLETE, this.onSecTimerCompleteHandler);
            }
            this._timer.reset();
            this._timer.start();
            return;
        }

        /* internal  */onSecTimerCompleteHandler(arg1: SecTimer): void
        {
            var loc1=null;
            if (AutoFightRule.isAutoFight()) 
            {
                loc1 = Cache.instance.scene.getNearlyPassPointTarget(false);
                if (loc1 == null) 
                {
                    loc1 = Cache.instance.scene.getNearlyPassPointTarget(true);
                }
                if (loc1 != null) 
                {
                    AIManager.onAutoPathAIControl(loc1, AIType.AI_AutoFight);
                }
            }
            this.stopTimer();
            return;
        }

        public stopTimer(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            RolePlayer.instance.removeEventListener(PlayerEvent.ON_WALK, this.onPlayerWalkHandler);
            return;
        }

        public static get instance(): MagicTowerAutoPath
        {
            if (!MagicTowerAutoPath._instance) 
            {
                MagicTowerAutoPath._instance = new MagicTowerAutoPath();
            }
            return MagicTowerAutoPath._instance;
        }

        /* internal */ /* var */_timer: SecTimer;

        /* internal */ static /* var */_instance: MagicTowerAutoPath;
    }
